package com.wep.fence.util;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class PageUtils {
    /**
     * 页码数据
     */
    private static final String PAGE_NUM = "pageNum";
    private static final Integer DEFAULT_PAGE = 1;
    /**
     * 页面数据数量
     */
    private static final String PAGE_SIZE = "pageSize";
    /**
     * 如果不传分页 默认查询全部
     */
    private static final Integer DEFAULT_SIZE = -1;
    /**
     * 排序 是否正序排列，默认 true
     */
    private static final String SORT_STR = "sort";
    private static final Boolean DEFAULT_SORT = true;
    private static final String SORT_NAME_STR = "sortName";

    public static PageUtils getInstance() {
        return SingletonHolder.pageUtils;
    }

    /**
     * 获取分页数据
     */
    public Page page(HttpServletRequest request) {
        Page page = new Page();
        page.setCurrent(getPages(request));
        page.setSize(getSize(request));
        List<OrderItem> orderItems = getOrders(getSort(request), getSortName(request));
        if (CollUtil.isNotEmpty(orderItems)) {
            page.setOrders(orderItems);
        }
        return page;
    }

    /**
     * 自定义设置分页数据
     */
    public Page getPage(Long pages, Long size, boolean sort, String sortName) {
        Page page = new Page();
        if (pages != null) {
            page.setCurrent(pages);
        }
        if (size != null) {
            page.setSize(size);
        }
        List<OrderItem> orderItems = getOrders(sort, sortName);
        if (CollUtil.isNotEmpty(orderItems)) {
            page.setOrders(orderItems);
        }
        return page;
    }

    /**
     * 排序
     */
    private List<OrderItem> getOrders(boolean sort, String sortName) {
        // 排序字段
        if (StrUtil.isBlank(sortName)) {
            return null;
        }

        List<OrderItem> orderItems = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn(sortName);
        orderItem.setAsc(sort);
        orderItems.add(orderItem);

        return orderItems;
    }

    /**
     * 获取page参数
     */
    private Integer getPages(HttpServletRequest request) {
        String page = request.getParameter(PAGE_NUM);
        return StrUtil.isBlank(page) ? DEFAULT_PAGE : Integer.valueOf(page);
    }

    /**
     * 获取size参数
     */
    private Integer getSize(HttpServletRequest request) {
        String page = request.getParameter(PAGE_SIZE);
        return StrUtil.isBlank(page) ? DEFAULT_SIZE : Integer.valueOf(page);
    }

    /**
     * 获取sortName参数
     */
    private String getSortName(HttpServletRequest request) {
        return request.getParameter(SORT_NAME_STR);
    }

    /**
     * 获取sort参数 是否正序排列，默认 true
     */
    private boolean getSort(HttpServletRequest request) {
        String str = request.getParameter(SORT_STR);
        return StrUtil.isBlank(str) ? DEFAULT_SORT : Boolean.valueOf(str);
    }

    private static class SingletonHolder {
        public final static PageUtils pageUtils = new PageUtils();
    }
}
